//
//  CZComposeToolBar.swift
//  weibo
//
//  Created by Apple on 16/3/18.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
//  toolBar的枚举类型
enum CZComposeToolBarType:Int {
    //  图片
    case Picture = 1
    //  话题
    case Mention = 2
    //  #
    case Trend = 3
    //  表情
    case Emotion = 4
    //  加号
    case Add = 5
}

class CZComposeToolBar: UIStackView {
    //  点击按钮回调的闭包
    var composeButtonClickClosure: ((type: CZComposeToolBarType)->())?
    //  表情按钮
    var emotionButton: UIButton?
    //  根据外界设置的值，设置显示表情或者键盘
    var isEmotionKeyBoard: Bool = true {
        didSet {
            var defaultImageName = "compose_emoticonbutton_background"
            if !isEmotionKeyBoard {
                defaultImageName = "compose_keyboardbutton_background"
            }
            emotionButton?.setImage(UIImage(named: defaultImageName), forState: .Normal)
            emotionButton?.setImage(UIImage(named: defaultImageName + "_highlighted"), forState: .Highlighted)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        //  
        axis = .Horizontal
        distribution = .FillEqually
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        emotionButton = addChildButton("compose_emoticonbutton_background", type: .Emotion)
        addChildButton("compose_add_background", type: .Add)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        backgroundColor = RandomColor()
    }
    //  添加子控件
    private func addChildButton(imageName: String, type: CZComposeToolBarType) -> UIButton{
        let button = UIButton()
        button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
        button.tag = type.rawValue
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        //  设置背景图片，但是需要取消点击的高亮效果
//        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        //  不显示高亮效果
//        button.adjustsImageWhenHighlighted = false
        //  还有一种方式就是把图片作为按钮的背景色,不设置背景图片
        button.backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        addArrangedSubview(button)
        return button
    }
    // MARK:    点击事件
    @objc private func buttonClick(button: UIButton) {
        composeButtonClickClosure?(type: CZComposeToolBarType(rawValue: button.tag)!)
    }
}
